import { System } from "./System";
import { Register } from "../register/Register";
import { ComponentType } from "../component/Component";
/**
 * @class
 * @extends w.system.System
 * @memberof w.system
 */
class LightSystem extends System {
    constructor(opts = {}) {
        super(opts);
    }

    run(event) {
        let light = event.target;
        // 环境光
        if (event.componentType === ComponentType.AmbientLight) {
            light.toGPU()
        }
        if (event.componentType === ComponentType.HemisLight) {
            light.toGPU()
        }
        // 精准光源
        if (event.componentType === ComponentType.PointLight
            || event.componentType === ComponentType.SpotLight
            || event.componentType === ComponentType.DirectionLight
            || event.componentType === ComponentType.AreaLight) {
            const pointLight = Register.instance.manager(ComponentType.PointLight).components;
            const spotLight = Register.instance.manager(ComponentType.SpotLight).components;
            const directionLight = Register.instance.manager(ComponentType.DirectionLight).components;
            const areaLight = Register.instance.manager(ComponentType.AreaLight).components;
            const punctualLight = [...pointLight.values(), ...spotLight.values(), ...directionLight.values(), ...areaLight.values()];
            const shadowLight = [...pointLight.values(), ...directionLight.values(),];
            // Object.assign(punctualLight, pointLight, spotLight, directionLight, areaLight);

            if (punctualLight.size === 0) {
                return;
            }
            let newHash=""
            for (let index = 0; index < shadowLight.length; index++) {
                const light = shadowLight[index];
                if(light.cast){
                    newHash=newHash+light.id
                }
            }
            const punctualLightComp = Register.instance.manager(ComponentType.PunctualLight).get('x');
            // if (directionLight.cast || pointLight.cast) {
            //     punctualLightComp.event.isShadow = true
            // }
            if(punctualLightComp.hash!=newHash){
                punctualLightComp.hash=newHash
                punctualLightComp.event.mode="layout"
            }else{
                punctualLightComp.event.mode="value"
            }
            punctualLightComp.punctualLights = punctualLight;
        }

    }
}

export { LightSystem }